<?php

App::uses('Right','Model');

class Role extends AppModel {
    
    // define model name
    var $name = 'Role';
     
    // define model schema
    var $schema = array(
        'parent_id'         => array('type' => 'int'),
        'hierarchy'         => array('type' => 'string'),
        'name'              => array('type' => 'string'),
        'description'       => array('type' => 'string'),
        'created'           => array('type' => 'datetime'),
        'deletedOn'         => array('type' => 'datetime'),
    );
    
    public function getActiveRights(&$scope,$id) {
        
        $this->availableRights = array();
        
        $dbRights = $scope->Right->find('all',array('conditions' => array('role_id' => $id,'deletedOn' => null)));
        foreach($dbRights as $right) {
            $this->availableRights[] = $right['Right']['right_id'];
        }
        
        return $this->availableRights;
    }
    
    public function modifyRights(&$scope,$roleId,$rights,$available) {
        
        // cannot override this rights
        $rights[AppRight::DASHBOARD] = 1;
        $rights[AppRight::SUPERADMIN] = 0;
        $lesserRoles = $this->getLesserRolesIds($roleId);
        
        foreach ($rights as $id => $value) {
            
            if (!in_array($id,$available)) {
                continue;
            }
            
            if ($value == 1) {
                $recordExists = $scope->Right->find('first',array('conditions' => array('role_id' => $roleId,'right_id' => $id,'deletedOn' => null)));
                if ($recordExists == null) {
                    $scope->Right->create();
                    $scope->Right->save(array('Right' => array('role_id' => $roleId,'right_id' => $id)));
                }
            } else if ($value == 0) {
                $scope->Right->deleteAll(array('role_id' => $lesserRoles,'deletedOn' => null));
                $scope->Right->deleteAll(array('role_id' => $roleId,'right_id' => $id,'deletedOn' => null));
            }
        }
    }
    
    public function getLesserRolesIds($id) {

        $lesserRolesIds = array();
        
        $lesserRoles = $this->find('all',array('conditions' => array('hierarchy LIKE' => "%|{$id}|%",'deletedOn' => null)));
        foreach($lesserRoles as $lesserRole) {
            $lesserRolesIds[] = $lesserRole['Role']['id'];
        }
        
        return $lesserRolesIds;
    }   
    
    // before deleting a role set all children roles as 
    public function removeFromHierarchy($parentRole,$id) {
        
        $lesserRoles = $this->find('all',array('conditions' => array('hierarchy LIKE' => "%|{$id}%",'deletedOn' => null)));
        foreach ($lesserRoles as $lesserRole) {
            
            $lesserRole['Role']['hierarchy'] = preg_replace("|{$lesserRole['Role']['id']}","", $lesserRole['Role']['hierarchy']);
            if ($lesserRole['Role']['parent_id'] == $id) {
                $lesserRole['Role']['parent_id'] = $parentRole['Role']['id'];
            }
            
            $this->updateAll(array('hierarchy' => $lesserRole['Role']['hierarchy'],'parent_id' => $lesserRole['Role']['parent_id']),array('id' => $lesserRole['Role']['id']));
        }
    }
    
    public function getParentRole($id) {
        
        $child = $this->getActiveRecord($id);
        
        return $this->getActiveRecord(@$child['Role']['parent_id']);
    }
    
    public function cleanRights(&$scope,$roleId) {
        $scope->Right->deleteAll(array('role_id' => $roleId));
    }
}   

?>